<!-- 工单管理 -->
<template>
  <el-dialog
    custom-class="subWorkOrder-dialog"
    append-to-body
    width="70%"
    top="5vh"
    title="工单拆分"
    :visible.sync="workorderSplit.visible"
    @open="workorderSplitOpen"
    @close="workorderSplitClose"
  >
    <div class="workerSplit-container">
      <div class="mainWorkOrder">
        <h1 class="subTitle">母工单信息</h1>
        <avue-form v-model="workorderSplit.masterWorkOrderModel" ref="basicInfoForm" key="basicInfoFormKey" :option="workorderSplit.masterWorkOrderOptions"> </avue-form>
      </div>
      <h1 class="subTitle">拆分工单</h1>
      <div class="sub-container">
        <!-- 拆分类型 -->
        <div class="subWorkType">
          <avue-form v-model="workorderSplit.splitTypeForm" :option="workorderSplit.typeOptions">
            <template slot-scope="{ disabled, size }" slot="splitType">
              <el-select v-model="workorderSplit.splitTypeForm.splitType" placeholder="请选择拆分类型" @change="changeSplittedType">
                <el-option label="数量" value="1"> </el-option>
                <el-option label="产品" value="2"> </el-option>
              </el-select>
            </template>
          </avue-form>
        </div>
        <div class="subWorkOrders" v-for="(item, index) in subWorkOrderCount" :key="index">
          <avue-form
            v-if="workorderSplit.subWork[index]"
            v-model="workorderSplit.subWork[index].splitWorkOrderModel"
            :ref="'subWorkRef' + index"
            :key="amountFormKey"
            :option="workorderSplit.subWork[index].splitWorkOrderOptions"
          >
            <!-- 产品编码 -->
            <template slot-scope="scope" slot="productPartNo">
              <el-cascader
                filterable
                :ref="'subCascaderRef' + index"
                v-model="workorderSplit.subWork[index].splitWorkOrderModel.partMasterIdArray"
                placeholder="请选择产品编码"
                :options="subPartNoList"
                @change="(value) => changeSubPartNo(value, index)"
              ></el-cascader>
            </template>
            <!-- 计划开始时间 -->
            <template slot-scope="scope" slot="planStartTime">
              <el-date-picker
                data-testid="sub-datetime"
                v-model="workorderSplit.subWork[index].splitWorkOrderModel.planStartTime"
                type="datetime"
                placeholder="请选择 计划开始时间"
                format="yyyy-MM-dd HH:mm:ss"
                valueFormat="yyyy-MM-dd HH:mm:ss"
              >
              </el-date-picker>
            </template>
            <!-- 计划完成时间 -->
            <template slot-scope="scope" slot="planEndTime">
              <el-date-picker
                v-model="workorderSplit.subWork[index].splitWorkOrderModel.planEndTime"
                type="datetime"
                placeholder="请选择 计划完成时间"
                format="yyyy-MM-dd HH:mm:ss"
                valueFormat="yyyy-MM-dd HH:mm:ss"
              >
              </el-date-picker>
            </template>
          </avue-form>
          <div class="splitWorkOrderDeleteBtn" v-if="index !== 0">
            <el-button type="danger" icon="el-icon-delete" circle @click="deleteSubWorkOrder(index)"> </el-button>
          </div>
        </div>
      </div>

      <div class="splitWorkOrderBtn" v-if="workorderSplit.splitTypeForm.splitType">
        <el-button type="primary" icon="el-icon-plus" circle @click="addSubWorkOrder"> </el-button>
      </div>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" size="small" class="splitBtn" icon="el-icon-circle-plus-outline" @click="submitForm" v-preventReClick>拆分</el-button>
      <el-button size="small" icon="el-icon-circle-close" class="splitCancelBtn" @click="cancelSubmitForm">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { workorderSplit, getDetail } from '@/api/product/workorder';
import { getBomDetailMenuTree } from '@/api/admin/bom';
import { mapGetters, mapState } from 'vuex';
import { compareTime, deepClone } from '@/util/util';
let reg = new RegExp('[\\u4E00-\\u9FFF]+', 'g');
const validateSubOrderNo = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请输入工单号'));
  } else if (value.length > 30) {
    callback(new Error('子工单号长度不可超过30个字符!'));
  } else if (reg.test(value)) {
    callback(new Error('子工单号出现汉字，请重新输入!'));
  } else {
    callback();
  }
};
export default {
  name: 'SubWorkOrder',
  data() {
    return {
      rowId: null,
      remainSubPartNoList: [],
      subPartNoList: [], // 上阶产品列表
      subWorkOrderIndex: 0, //子工单索引
      subWorkOrderCount: 1, // 子工单个数
      workorderSplit: {
        splitTypeForm: {
          splitType: ''
        },
        typeOptions: {
          submitBtn: false,
          emptyBtn: false,
          labelWidth: 120,
          column: [
            {
              label: '拆分类型',
              prop: 'splitType',
              formslot: true,
              span: 7,
              rules: [
                {
                  required: true,
                  message: '请选择拆分类型',
                  trigger: 'change'
                }
              ]
            }
          ]
        },
        visible: false,
        masterWorkOrderModel: {},
        masterWorkOrderOptions: {
          submitBtn: false,
          emptyBtn: false,
          labelWidth: 120,
          column: [
            {
              label: '生产订单',
              prop: 'productionOrderNo',
              span: 7,
              search: true,
              disabled: true
            },
            {
              label: '工单号',
              prop: 'workOrderNo',
              span: 7,
              disabled: true
            },
            {
              label: '产品编码',
              prop: 'productPartNo',
              span: 7,
              dicData: [],
              disabled: true,
              placeholder: '请选择 产品编码'
            },
            {
              label: '产品名称',
              prop: 'productPartName',
              span: 7,
              search: true,
              disabled: true
            },
            {
              label: '所属机型',
              prop: 'productPartModel',
              span: 7,
              disabled: true
            },
            {
              label: '生产批次',
              prop: 'lotNo',
              span: 7,
              disabled: true
            },
            {
              label: '工单计划数量',
              prop: 'planQuantity',
              span: 7,
              type: 'number',
              disabled: true,
              minRows: 1
            },
            {
              label: '计划开始时间',
              prop: 'planStartTime',
              disabled: true,
              span: 7,
              type: 'datetime',
              format: 'yyyy-MM-dd HH:mm:ss',
              valueFormat: 'yyyy-MM-dd HH:mm:ss'
            },
            {
              label: '计划完成时间',
              prop: 'planEndTime',
              disabled: true,
              span: 7,
              type: 'datetime',
              format: 'yyyy-MM-dd HH:mm:ss',
              valueFormat: 'yyyy-MM-dd HH:mm:ss'
            }
          ]
        },
        subWork: [
          {
            splitWorkOrderModel: {},
            splitWorkOrderOptions: {
              submitBtn: false,
              emptyBtn: false,
              labelWidth: 120,
              column: []
            }
          }
        ]
      },
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      }
    };
  },
  computed: {
    ...mapGetters(['permission']),
    ...mapState({
      realName: (state) => state.user.userInfo.real_name,
      userId: (state) => state.user.userInfo.user_id
    }),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.workorder_add, false),
        viewBtn: this.vaildData(this.permission.workorder_view, false),
        delBtn: this.vaildData(this.permission.workorder_delete, false),
        editBtn: this.vaildData(this.permission.workorder_edit, false)
      };
    }
  },
  methods: {
    open(id) {
      this.workorderSplit.visible = true;
      this.workorderSplit.splitTypeForm.splitType = '1';
      this.rowId = id;
    },
    workorderSplitClose() {
      const that = this;
      let subFormCount = that.workorderSplit.subWork.length;
      let promiseCount = 0;
      for (let i = 0; i < subFormCount; i++) {
        that.$refs[`subWorkRef${i}`][0].resetFields();
        promiseCount++;
        if (promiseCount == subFormCount) {
          this.$emit('closeSubWorkOrderDialog');
        }
      }
    },
    workorderSplitOpen() {
      const that = this;
      // 清除旧数据
      that.subWorkOrderIndex = 0;
      that.subWorkOrderCount = 1;
      that.subPartNoList = [];
      const checkStartTime = (rule, value, callback) => {
        let index = rule.index;
        let planEndTime = that.workorderSplit.subWork[index].splitWorkOrderModel.planEndTime;
        if (!value) {
          callback(new Error('请输入计划开始时间'));
        }
        if (!planEndTime) {
          callback();
        } else if (planEndTime && value) {
          let result = compareTime(planEndTime, value);
          if (result === 2) {
            callback(new Error('计划开始时间不能大于计划完成时间'));
          } else {
            callback();
          }
        } else {
          callback();
        }
      };
      const checkEndTime = (rule, value, callback) => {
        let index = rule.index;
        let planStartTime = that.workorderSplit.subWork[index].splitWorkOrderModel.planStartTime;
        if (!value) {
          callback(new Error('请输入计划完成时间'));
        }
        if (!planStartTime) {
          callback();
        } else if (planStartTime && value) {
          let result = compareTime(planStartTime, value);
          if (result === 1) {
            callback(new Error('计划完成时间不能小于计划开始时间'));
          } else {
            callback();
          }
        } else {
          callback();
        }
      };
      for (const key in that.workorderSplit.masterWorkOrderModel) {
        that.workorderSplit.masterWorkOrderModel[key] = '';
      }
      that.workorderSplit.subWork = [
        {
          splitWorkOrderModel: {
            partMasterId: [],
            productPartName: '',
            productPartNo: '',
            productPartModel: '',
            workOrderNo: '',
            lotNo: '',
            planStartTime: '',
            planEndTime: ''
          },
          splitWorkOrderOptions: {
            submitBtn: false,
            emptyBtn: false,
            labelWidth: 120,
            column: [
              {
                label: '工单号',
                prop: 'workOrderNo',
                span: 7,
                disabled: false,
                display: true,
                rules: [{ validator: validateSubOrderNo, trigger: 'blur' }]
              },
              {
                label: '产品编码',
                prop: 'productPartNo',
                span: 7,
                display: false,
                rules: [
                  {
                    required: true,
                    message: '请选择产品编码',
                    trigger: 'change'
                  }
                ],
                placeholder: '请选择 产品编码'
              },
              {
                label: '产品名称',
                prop: 'productPartName',
                span: 7,
                disabled: true,
                display: false,
                search: true
              },
              {
                label: '工单计划数量',
                prop: 'planQuantity',
                span: 7,
                type: 'number',
                minRows: 1,
                display: true,
                rules: [
                  {
                    required: true,
                    message: '请输入工单计划数量',
                    trigger: 'blur'
                  }
                ]
              },
              {
                label: '计划开始时间',
                prop: 'planStartTime',
                display: true,
                span: 7,
                rules: [
                  {
                    required: true,
                    validator: checkStartTime,
                    index: 0,
                    trigger: 'change'
                  }
                ]
              },
              {
                label: '计划完成时间',
                span: 7,
                prop: 'planEndTime',
                display: true,
                rules: [
                  {
                    required: true,
                    validator: checkEndTime,
                    index: 0,
                    trigger: 'change'
                  }
                ]
              }
            ]
          }
        }
      ];
      if (that.$refs['subWorkRef0'] && that.$refs['subWorkRef0'].length > 0) {
        that.$refs['subWorkRef0'][0].resetFields();
      }
      getDetail(that.rowId).then((res) => {
        let data = res.data.data;
        // 母工单信息赋值
        that.workorderSplit.masterWorkOrderModel = structuredClone(data);

        that.getPartNoListByBom(); // 根据产品 获取 产品编码列表

        // 初始化子工单
        that.initSubWorkOrder();
      });
    },
    initSubWorkOrder() {
      this.workorderSplit.subWork.forEach((work, index) => {
        work.splitWorkOrderModel.workOrderNo = this.workorderSplit.masterWorkOrderModel.workOrderNo + '-' + (index + 1);
        if (!work.splitWorkOrderModel.planQuantity) {
          work.splitWorkOrderModel.planQuantity = this.workorderSplit.masterWorkOrderModel.originalPlanQuantity;
        }
      });
    },
    addSubWorkOrder() {
      const that = this;
      that.subWorkOrderCount++;
      const checkStartTime = (rule, value, callback) => {
        let index = rule.index;
        let planEndTime = that.workorderSplit.subWork[index].splitWorkOrderModel.planEndTime;
        if (!value) {
          callback(new Error('请输入计划开始时间'));
        }
        if (!planEndTime) {
          callback();
        } else if (planEndTime && value) {
          let result = compareTime(planEndTime, value);
          if (result === 2) {
            callback(new Error('计划开始时间不能大于计划完成时间'));
          } else {
            callback();
          }
        } else {
          callback();
        }
      };
      const checkEndTime = (rule, value, callback) => {
        let index = rule.index;
        let planStartTime = that.workorderSplit.subWork[index].splitWorkOrderModel.planStartTime;
        if (!value) {
          callback(new Error('请输入计划完成时间'));
        }
        if (!planStartTime) {
          callback();
        } else if (planStartTime && value) {
          let result = compareTime(planStartTime, value);
          if (result === 1) {
            callback(new Error('计划完成时间不能小于计划开始时间'));
          } else {
            callback();
          }
        } else {
          callback();
        }
      };
      // splitType 1-数量 2-产品
      if (that.workorderSplit.splitTypeForm.splitType == 1) {
        that.workorderSplit.subWork.push({
          splitWorkOrderModel: {},
          splitWorkOrderOptions: {
            submitBtn: false,
            emptyBtn: false,
            labelWidth: 120,
            column: [
              {
                label: '工单号',
                prop: 'workOrderNo',
                span: 7,
                disabled: false,
                display: true,
                rules: [{ validator: validateSubOrderNo, trigger: 'blur' }]
              },
              {
                label: '产品编码',
                prop: 'productPartNo',
                span: 7,
                display: false,
                rules: [
                  {
                    required: true,
                    message: '请选择产品编码',
                    trigger: 'change'
                  }
                ],
                placeholder: '请选择 产品编码'
              },
              {
                label: '产品名称',
                prop: 'productPartName',
                span: 7,
                display: false,
                disabled: true,
                search: true
              },
              {
                label: '工单计划数量',
                prop: 'planQuantity',
                span: 7,
                type: 'number',
                minRows: 1,
                display: true,
                rules: [
                  {
                    required: true,
                    message: '请输入工单计划数量',
                    trigger: 'blur'
                  }
                ]
              },
              {
                label: '计划开始时间',
                prop: 'planStartTime',
                span: 7,
                display: true,
                rules: [
                  {
                    required: true,
                    validator: checkStartTime,
                    index: that.subWorkOrderCount - 1,
                    trigger: 'change'
                  }
                ]
              },
              {
                label: '计划完成时间',
                span: 7,
                prop: 'planEndTime',
                display: true,
                rules: [
                  {
                    required: true,
                    validator: checkEndTime,
                    index: that.subWorkOrderCount - 1,
                    trigger: 'change'
                  }
                ]
              }
            ]
          }
        });
      } else if (that.workorderSplit.splitTypeForm.splitType == 2) {
        that.workorderSplit.subWork.push({
          splitWorkOrderModel: {},
          splitWorkOrderOptions: {
            submitBtn: false,
            emptyBtn: false,
            labelWidth: 120,
            column: [
              {
                label: '工单号',
                prop: 'workOrderNo',
                span: 7,
                disabled: false,
                display: true,
                rules: [{ validator: validateSubOrderNo, trigger: 'blur' }]
              },
              {
                label: '产品编码',
                prop: 'productPartNo',
                span: 7,
                display: true,
                rules: [
                  {
                    required: true,
                    message: '请选择产品编码',
                    trigger: 'change'
                  }
                ],
                placeholder: '请选择 产品编码'
              },
              {
                label: '产品名称',
                prop: 'productPartName',
                span: 7,
                display: true,
                disabled: true,
                search: true
              },
              {
                label: '工单计划数量',
                prop: 'planQuantity',
                span: 7,
                type: 'number',
                display: true,
                minRows: 1,
                rules: [
                  {
                    required: true,
                    message: '请输入工单计划数量',
                    trigger: 'blur'
                  }
                ]
              },
              {
                label: '计划开始时间',
                prop: 'planStartTime',
                span: 7,
                display: true,
                rules: [
                  {
                    required: true,
                    validator: checkStartTime,
                    index: that.subWorkOrderCount - 1,
                    trigger: 'change'
                  }
                ]
              },
              {
                label: '计划完成时间',
                span: 7,
                prop: 'planEndTime',
                display: true,
                rules: [
                  {
                    required: true,
                    validator: checkEndTime,
                    index: that.subWorkOrderCount - 1,
                    trigger: 'change'
                  }
                ]
              }
            ]
          }
        });
      }
      that.workorderSplit.subWork.forEach((work, index) => {
        work.splitWorkOrderModel.workOrderNo = work.splitWorkOrderModel.workOrderNo || that.workorderSplit.masterWorkOrderModel.workOrderNo + '-' + (index + 1);
        if (!work.splitWorkOrderModel.planQuantity) {
          work.splitWorkOrderModel.planQuantity = that.workorderSplit.masterWorkOrderModel.originalPlanQuantity;
        }
        if (that.workorderSplit.splitTypeForm.splitType == 1) {
          work.splitWorkOrderModel.partMasterId = that.workorderSplit.masterWorkOrderModel.partMasterId;
        }
      });
    },
    deleteSubWorkOrder(index) {
      this.subWorkOrderCount--;
      this.workorderSplit.subWork.splice(index, 1);
      console.log('this.workorderSplit.subWork', this.workorderSplit.subWork);
    },
    duplicates(arr) {
      let temp = [];
      arr.forEach((item) => {
        if (arr.indexOf(item) != arr.lastIndexOf(item) && temp.indexOf(item) == -1) {
          temp.push(item);
        }
      });
      return temp;
    },
    submitForm() {
      const that = this;
      let subFormCount = that.workorderSplit.subWork.length;
      let splitWorkOrderModel = that.workorderSplit.subWork.map((s) => s.splitWorkOrderModel.workOrderNo);
      let result = this.duplicates([...splitWorkOrderModel, that.workorderSplit.masterWorkOrderModel.workOrderNo]);
      if (result.length > 0) {
        this.$message.warning(`工单${result[0]}出现重复，请重新输入！`);
        return false;
      }
      let promiseCount = 0;
      for (let i = 0; i < subFormCount; i++) {
        that.$refs[`subWorkRef${i}`][0].validate((valid, done) => {
          if (valid) {
            promiseCount++;
            if (promiseCount == subFormCount) {
              // 验证通过
              let masterWorkOrder = that.workorderSplit.masterWorkOrderModel;
              let splitWorkOrderModel = that.workorderSplit.subWork.map((s) => s.splitWorkOrderModel);
              splitWorkOrderModel = splitWorkOrderModel.map((subwork) => {
                return {
                  splitType: that.workorderSplit.splitTypeForm.splitType,
                  productPartName: that.workorderSplit.splitTypeForm.splitType == 1 ? masterWorkOrder.productPartName : subwork.productPartName,
                  productPartNo: that.workorderSplit.splitTypeForm.splitType == 1 ? masterWorkOrder.productPartNo : subwork.productPartNo,
                  productPartModel: that.workorderSplit.splitTypeForm.splitType == 1 ? masterWorkOrder.productPartModel : subwork.productPartModel, // 产品型号
                  productPartId: that.workorderSplit.splitTypeForm.splitType == 1 ? masterWorkOrder.productPartId : subwork.productPartId,
                  productionOrderId: masterWorkOrder.productionOrderId, //生产订单ID
                  productionOrderNo: masterWorkOrder.productionOrderNo, //	客户生产订单号
                  parentWorkOrderId: masterWorkOrder.id, // 被拆分工单ID
                  parentWorkOrderNo: masterWorkOrder.workOrderNo, // 拆分工单前母工单号
                  workOrderNo: subwork.workOrderNo,
                  planQuantity: masterWorkOrder.planQuantity,
                  splitQuantity: subwork.planQuantity,
                  planStartTime: subwork.planStartTime,
                  planEndTime: subwork.planEndTime,
                  salesOrderNo: masterWorkOrder.salesOrderNo, // 销售订单号
                  type: masterWorkOrder.type, // 工单类型
                  customerName: masterWorkOrder.customerName, // 客户名称
                  customerId: masterWorkOrder.customerId, // 客户ID
                  customerCode: masterWorkOrder.customerCode, // 客户编码
                  lotNo: masterWorkOrder.lotNo, //生产批次
                  remark: masterWorkOrder.remark //备注
                };
              });
              console.log('splitWorkOrderModel', splitWorkOrderModel);
              workorderSplit(splitWorkOrderModel)
                .then(() => {
                  that.workorderSplit.visible = false;
                  that.$message.success('拆分成功');
                  that.$emit('closeSubWorkOrderDialog', true);
                })
                .catch(() => {});
            }
          }
          done();
        });
      }
    },
    cancelSubmitForm() {
      this.workorderSplit.visible = false;
      this.$emit('closeSubWorkOrderDialog');
    },
    changeSubPartNo(value, index) {
      let subCascaderRef = this.$refs['subCascaderRef' + index];
      if (subCascaderRef && subCascaderRef[0]) {
        let checkNodes = subCascaderRef[0].getCheckedNodes()[0].data;
        console.log('checkNodes', checkNodes);
        if (checkNodes) {
          this.$set(this.workorderSplit.subWork[index].splitWorkOrderModel, 'partMasterIdArray', value);
          this.$set(this.workorderSplit.subWork[index].splitWorkOrderModel, 'partMasterId', [checkNodes.value]);
          this.$set(this.workorderSplit.subWork[index].splitWorkOrderModel, 'productPartName', checkNodes.partName);
          this.$set(this.workorderSplit.subWork[index].splitWorkOrderModel, 'productPartNo', checkNodes.partNo);
          this.$set(this.workorderSplit.subWork[index].splitWorkOrderModel, 'productPartModel', checkNodes.partModel);
          this.$set(this.workorderSplit.subWork[index].splitWorkOrderModel, 'productPartId', checkNodes.value);
        }
      }
    },
    changeSplittedType(value) {
      switch (value) {
        case '1':
          this.getPartNoListByBom();
          break;
        case '2':
          this.getPartNoListByBom();
          break;
        default:
          break;
      }
      this.workorderSplit.subWork.forEach((item) => {
        let partNoIndex = item.splitWorkOrderOptions.column.findIndex((cItem) => cItem.prop == 'productPartNo');
        let partNameIndex = item.splitWorkOrderOptions.column.findIndex((cItem) => cItem.prop == 'productPartName');
        // splitType 1-数量 2-产品
        switch (value) {
          case '1': // 数量
            // 产品编码，产品名称隐藏
            if (partNoIndex > -1) {
              this.$set(item.splitWorkOrderOptions.column[partNoIndex], 'display', false);
            }
            if (partNameIndex > -1) {
              this.$set(item.splitWorkOrderOptions.column[partNameIndex], 'display', false);
            }
            break;
          case '2': // 产品
            if (partNoIndex > -1) {
              this.$set(item.splitWorkOrderOptions.column[partNoIndex], 'display', true);
            }
            if (partNameIndex > -1) {
              this.$set(item.splitWorkOrderOptions.column[partNameIndex], 'display', true);
            }
            break;
          default:
            break;
        }
      });
      this.workorderSplit.subWork.forEach((work, index) => {
        work.splitWorkOrderModel.planQuantity = this.workorderSplit.masterWorkOrderModel.originalPlanQuantity;
        if (value == 1) {
          work.splitWorkOrderModel.partMasterId = this.workorderSplit.masterWorkOrderModel.partMasterId;
        } else {
          work.splitWorkOrderModel.partMasterId = [];
          work.splitWorkOrderModel.partMasterIdArray = [];
          work.splitWorkOrderModel.productPartName = '';
          work.splitWorkOrderModel.productPartNo = '';
          work.splitWorkOrderModel.productPartModel = '';
          work.splitWorkOrderModel.productPartId = '';
        }
      });
      console.log('workorderSplit.subWork', this.workorderSplit.subWork);
    },
    // 根据产品 获取 产品编码列表
    getPartNoListByBom() {
      const that = this;
      // that.subPartNoList = [
      //   {
      //     label: 'ctx-1',
      //     value: '111',
      //     partName: 'ctx-1',
      //     partNo: 'ctx-1',
      //     partModel: 'ctx-1'
      //   },
      //   {
      //     label: 'ctx-2',
      //     value: '222',
      //     partName: 'ctx-2',
      //     partNo: 'ctx-2',
      //     partModel: 'ctx-2'
      //   }
      // ];
      // that.remainSubPartNoList = deepClone(that.subPartNoList);
      if (that.workorderSplit.masterWorkOrderModel.bomId) {
        getBomDetailMenuTree(that.workorderSplit.masterWorkOrderModel.bomId)
          .then((res) => {
            let data = res.data.data;
            let result = formatterTreeData(data);

            function formatterTreeData(data) {
              let result = [];
              for (let i = 0; i < data.length; i++) {
                const item = data[i];
                if (item.children && item.children.length > 0) {
                  result.push({
                    label: item.partNo,
                    value: item.partMasterId,
                    partName: item.partName,
                    partNo: item.partNo,
                    partModel: item.partModel,
                    children: formatterTreeData(item.children)
                  });
                } else {
                  result.push({
                    label: item.partNo,
                    value: item.partMasterId,
                    partName: item.partName,
                    partNo: item.partNo,
                    partModel: item.partModel
                  });
                }
              }
              return result;
            }

            that.subPartNoList = result;
            that.remainSubPartNoList = deepClone(that.subPartNoList);
          })
          .catch((err) => {});
      }
    }
  }
};
</script>

<style lang="scss" scoped>
::v-deep .subWorkOrder-dialog {
  .el-dialog__body {
    padding: 0 20px;
  }
  .workerSplit-container {
    max-height: 700px;
    overflow-y: auto;
    .subTitle {
      font-size: 16px;
      font-weight: bold;
      color: rgba(0, 0, 0, 0.85);
      border-bottom: 1px solid #eee;
      padding-bottom: 10px;
    }
    .sub-container {
      .subWorkOrders {
        position: relative;
        .splitWorkOrderDeleteBtn {
          position: absolute;
          right: 70px;
          top: 0;
        }
      }
    }
    .splitWorkOrderBtn {
      display: flex;
      justify-content: center;
    }
  }
}
</style>
